package com.first.nell.utils.analytics.util

import android.app.Application
import com.google.android.gms.tasks.Task
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.analytics.ktx.logEvent
import com.google.firebase.ktx.Firebase
import com.first.nell.utils.analytics.bean.ParamFirebaseS2S
import com.first.nell.utils.analytics.event.IAnalyticsEvent
import com.first.nell.utils.analytics.http.HttpHelper
import com.google.gson.Gson
import com.first.nell.network.ApiUrl


object FirebaseUtil {

    /** 句柄 */
//    private var mFirebaseAnalytics: FirebaseAnalytics = Firebase.analytics
//
    /** firebase app id, 服务端回推firebase事件使用 */
    private var mFirebaseAppId: String = ""

    /** ga4 instance id, 服务端回推firebase事件使用 */
    private var mGa4InstanceId: String = ""

    /**
     * 初始化
     *
     * @param context Application
     */
    fun init(context: Application) {
//        try {
//            if (mFirebaseAppId.isBlank()) {
//                Thread {
//                    mFirebaseAppId = FirebaseAnalytics.getInstance(context).firebaseInstanceId
//                    println("mFirebaseAppId=$mFirebaseAppId")
//                }.start()
//            }
//
//            if (mGa4InstanceId.isBlank()) {
//                val gResult: Task<String> = FirebaseAnalytics.getInstance(context).appInstanceId
//                gResult.addOnSuccessListener {
//                    mGa4InstanceId = it
//                    println("mGa4InstanceId=$mGa4InstanceId")
//                }
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
    }

    /**
     * 打点
     *
     * @param event 事件名
     */
    fun addEvent(event: IAnalyticsEvent) {
//        try {
//            when (event) {
//                IAnalyticsEvent.AT_REGISTER -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SIGN_UP) {
//                        param(FirebaseAnalytics.Param.METHOD, IAnalyticsEvent.AT_REGISTER.name)
//                    }
//                }
//                IAnalyticsEvent.AT_BASIC -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP) {
//                        param(
//                            FirebaseAnalytics.Param.CONTENT_TYPE,
//                            IAnalyticsEvent.AT_BASIC.name
//                        )
//                        param(FirebaseAnalytics.Param.LEVEL, "1")
//                        param(FirebaseAnalytics.Param.SCORE, "100")
//                    }
//                }
//                IAnalyticsEvent.AT_WORK -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.POST_SCORE) {
//                        param(FirebaseAnalytics.Param.CONTENT_TYPE, IAnalyticsEvent.AT_WORK.name)
//                        param(FirebaseAnalytics.Param.CHARACTER, IAnalyticsEvent.AT_WORK.name)
//                        param(FirebaseAnalytics.Param.LEVEL, "1")
//                        param(FirebaseAnalytics.Param.SCORE, "100")
//                    }
//                }
//                IAnalyticsEvent.AT_CONTACT -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_SEARCH_RESULTS) {
//                        param(
//                            FirebaseAnalytics.Param.CONTENT_TYPE,
//                            IAnalyticsEvent.AT_CONTACT.name
//                        )
//                        param(
//                            FirebaseAnalytics.Param.SEARCH_TERM,
//                            IAnalyticsEvent.AT_CONTACT.name
//                        )
//                    }
//                }
//                IAnalyticsEvent.AT_REV_CARD -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM) {
//                        param(
//                            FirebaseAnalytics.Param.CONTENT_TYPE,
//                            IAnalyticsEvent.AT_REV_CARD.name
//                        )
//                    }
//                }
//                IAnalyticsEvent.AT_LOAN_APPLY -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.PURCHASE) {
//                        param(
//                            FirebaseAnalytics.Param.CONTENT_TYPE,
//                            IAnalyticsEvent.AT_LOAN_APPLY.name
//                        )
//                        param(
//                            FirebaseAnalytics.Param.AFFILIATION,
//                            IAnalyticsEvent.AT_LOAN_APPLY.name
//                        )
//                        param(FirebaseAnalytics.Param.COUPON, IAnalyticsEvent.AT_LOAN_APPLY.name)
//                        param(
//                            FirebaseAnalytics.Param.TRANSACTION_ID,
//                            IAnalyticsEvent.AT_LOAN_APPLY.name
//                        )
//                        param(FirebaseAnalytics.Param.VALUE, "1")
//                        param(FirebaseAnalytics.Param.SHIPPING, "1")
//                        param(FirebaseAnalytics.Param.TAX, "1")
//                        param(FirebaseAnalytics.Param.CURRENCY, "USD")
//                    }
//                }
//                IAnalyticsEvent.AT_REPAY -> {
//                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.TUTORIAL_COMPLETE) {
//                        param(
//                            FirebaseAnalytics.Param.CONTENT_TYPE,
//                            IAnalyticsEvent.AT_REPAY.name
//                        )
//                        param(FirebaseAnalytics.Param.ITEM_ID, IAnalyticsEvent.AT_REPAY.name)
//                    }
//                }
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
    }

    /**
     * 保存firebase s2s信息
     */
    fun saveS2SInfo(targetUrlList: List<String>) {
//        val params = ParamFirebaseS2S()
//        val body = Gson().toJson(params).trim()
//        println("====== $targetUrlList")
//
//        //传多国时，里触发多次
//        val apiPath = ApiUrl.NARSCASH_PATH_FIREBASE_S2S
//        targetUrlList.forEach { host ->
//            val pathFormat =
//                if (host.endsWith(apiPath)) host else (host + apiPath)
//            HttpHelper.postData(pathFormat, body)
//        }
    }

    /**
     * 获取Firebase App Id
     */
    fun getFirebaseAppId(): String {
        return mFirebaseAppId
    }

    /**
     * 获取Firebase Ga4 instance Id
     */
    fun getGa4InstanceId(): String {
        return mGa4InstanceId
    }
}